/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.cam.ccn.sac.modules.contacts;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import uk.ac.cam.ccn.sac.modules.audio.streaming.AudioStreamsManager;
import uk.ac.cam.ccn.sac.net.CCNNet;
import uk.ac.cam.ccn.sac.net.CCNNet.ContactCallBack;

/**
 *
 * @author Thomas
 */
public class ContactsElement extends JFrame implements ContactCallBack,ListSelectionListener {
    private DefaultListModel listModel = new DefaultListModel();
    private JList list = new JList(listModel);
    private final JButton listen = new JButton("Listen");
    private final JButton stop = new JButton("Stop");
    private Map<String, Contact> contacts = new HashMap<String, Contact>();
    private Contact selected=null; 
    
    public ContactsElement(){
        super("Contacts");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        list.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
        list.addListSelectionListener(this);
        
        Container content = getContentPane();
        content.add(new JScrollPane(list), BorderLayout.NORTH);
        
        JPanel bp = new JPanel();
        ActionListener listenListener = 
        new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    if(selected!=null)
                        AudioStreamsManager.getInstance().subAudioStream(selected);
                }catch(Exception ex){
                    System.out.println(ex.getMessage());
                }
            }
        };
        listen.addActionListener(listenListener);
        bp.add(listen, BorderLayout.CENTER);
        
        ActionListener stopListener = 
        new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    if(selected!=null)
                        AudioStreamsManager.getInstance().unsubAudioStream(selected);
                }catch(Exception ex){
                    System.out.println(ex.getMessage());
                }
            }
        };
        stop.addActionListener(stopListener);
        bp.add(stop, BorderLayout.SOUTH);
        
        content.add(bp, BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(300, 400);
    }

    @Override
    public void contactReceived(Contact contact) {
        String name = contact.getAttribute(Contact.USER_NAME);
        if(contact.getAttribute(Contact.AUDIO_STREAM)!=null)
            name += "(audio)";
        if(contact.getAttribute(Contact.PRIVATE_MESSAGE)!=null)
            name +="(private message)";
        listModel.addElement(name);
        contacts.put(name, contact);
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
            JList l = (JList)e.getSource();
            String value = (String)l.getSelectedValue();
            if(value==null)
                return;
            value.replace("(audio)", "");
            value.replace("(private message)", "");
            
            System.out.println(value+" has been selected");
            if(contacts.containsKey(value)){
                if(contacts.get(value).getAttribute(Contact.AUDIO_STREAM) !=null){
                    selected = contacts.get(value);
                }else{
                    System.out.println("this user does not have an audio stream");
                }                
            }
        }
    }
    
    
}
